package cn.jofei.server.http.entity;

import com.alibaba.fastjson2.JSON;

import java.io.Serializable;

/**
 * 响应体
 *
 * @author Jofei Bao
 * @version bao-cloud v1.0.0
 * @date 2022年06月11日 21:12
 */
public class R<T> implements Serializable {
    /**
     * 响应状态码
     */
    private Integer status;
    private String msg;
    private T data;

    public static <T> R<T> fail() {
        return new R<>(201, "服务器处理失败", null);
    }

    public static <T> R<T> fail(String msg) {
        return new R<>(201, msg, null);
    }

    public static <T> R<T> fail(String msg, T data) {
        return new R<>(201, msg, data);
    }

    public static <T> R<T> success() {
        return new R<>(200, "服务器处理成功！", null);
    }

    public static <T> R<T> success(T obj) {
        if (obj instanceof String) {
            return new R<>(200, (String) obj, null);
        } else {
            return new R<>(200, "服务器处理成功！", obj);
        }
    }

    public static <T> R<T> success(String msg, T obj) {
        return new R<>(200, msg, obj);
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public R(Integer status, String msg, T data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }
}
